package com.weetop.aklyscan.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.weetop.aklyscan.R;

import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class EditDialog extends Dialog {
    private Context mContext;
    private ImageView back;
    private EditText edit;
    private TextView button;
    private TextView text;
    private float weight;
    private boolean select;

    public EditDialog(Context context, TextView text, float weight, boolean select) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_edit);
        mContext = context;
        this.text = text;
        this.weight = weight;
        this.select = select;
        initVar();
        initView();
        initAttr();
        initAction();
    }

    public void initVar() {
    }

    public void initView() {
        back = findViewById(R.id.back);
        edit = findViewById(R.id.edit);
        button = findViewById(R.id.button);
    }

    public void initAttr() {
    }

    public void initAction() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edit.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(mContext, "输入不能为空");
                    return;
                }
                float content = Float.valueOf(edit.getText().toString().trim());
                if (!select && weight > content) {
                    AlertDialog.Builder normalDialog = new AlertDialog.Builder(mContext);
                    normalDialog.setTitle("提示");
                    normalDialog.setMessage("重量小于净重，是否保留？");
                    normalDialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            text.setText(edit.getText().toString().trim() + "g");
                            dialog.dismiss();
                            dismiss();
                        }
                    });
                    normalDialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            dismiss();
                        }
                    });
                    normalDialog.create().show();
                    return;
                }else{
                    text.setText(edit.getText().toString().trim() + "g");
                    dismiss();
                }
            }
        });
    }

    public static void setImage(String imageStr, ImageView image) {
        byte[] decode = Base64.decode(imageStr, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        image.setImageBitmap(bitmap);
    }
}
